/**
 * @fileOverview svg容器
 */

module.exports = gg.createClass({
    attributes: {
        width: {
            label: '宽度',
            type: 'datatype.Number',
            defaultValue: 800,
            setter: function(v) {
                this.node.setAttribute('width', v);
            },
            getter: function() {
                return this.node.getAttribute('width');
            }
        },
        height: {
            label: '高度',
            type: 'datatype.Number',
            defaultValue: 600,
            setter: function(v) {
                this.node.setAttribute('height', v);
            },
            getter: function() {
                return this.node.getAttribute('height');
            }
        }
    },
    methods: {
        /**
         * @override
         */
        createNode: function() {
            var dom = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
            return dom;
        },
        /**
         * @override
         */
        destroyNode: function() {
            this.node.remove();
        },
        addChild: function(attr, index) {
            var obj;
            var childType = this.childType();
            var childClass = gg.module(childType);
            if (attr instanceof childClass) {
                obj = attr;
                obj.parent = this;
                obj.removeFromParent();
            } else {
                if (!attr.classType) {
                    attr.classType = View.classType;
                }
                obj = gg.object(attr, this);
            }
            if (!(obj instanceof View)) {
                gg.error("addChild fail:obj instance is not view.jq.base");
            }
            if (index == null || index >= this.children.length) {
                this.children.push(obj);
                this.node.append(obj.node);
            } else {
                this.children[i].node.before(obj.node);
                this.children.splice(i, 0, obj);
            }
        },
        removeChild: function(obj) {
            for (var i = 0; i < this.children.length; i++) {
                if (obj === this.children[i]) {
                    return this.removeChildAt(i);
                }
            }
        },
        removeChildAt: function(index) {
            var obj = this.children[index];
            if (obj && obj.node) {
                debugger
                this.node.remove(obj.node);
            }
            this.children.splice(index, 1);
        },
        removeFromParent: function() {
            this.node.remove();
            if (this.parent) {
                this.parent.removeChild(this);
            }
        }
    },
    events: {
        event1: '事件1'
    }
}, 'ui.Base', []);